/**
 * 
 */
package com.nd.android.payment.ui.task;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.AsyncTask;
import android.view.KeyEvent;

import com.common.StarAppConfig;
import com.common.StarAppConst;
import com.common.allCommon.CommUtil;
import com.common.widget.StarappWaitDlg;
import com.nd.android.payment.R;
import com.nd.android.payment.business.operateImp.PayRechargeOperateImpl;
import com.nd.android.payment.controller.PaymentFactory;
import com.nd.android.payment.controller.IBean.IPayInfoBean;
import com.nd.android.payment.controller.IBean.IWealthInfoBean;
import com.nd.android.payment.controller.IOperate.IPayRechargeOperate;
import com.product.android.business.ApplicationVariable;
import com.product.android.utils.LogUtils;

/**
 * 支付任务
 * <br>
 * Created 2014-11-5 下午7:52:32
 * 
 * @version
 * @author zhangqr
 * 
 * @see
 */

public class CommonBuyTask extends AsyncTask<Integer, Integer, Object> {
    
    private static final String TAG = CommonBuyTask.class.getSimpleName();

    /**
     * 兑换金币
     */
    public static final int FROM_BUY_GOLD = 1;
    /**
     * 充值星钻
     */
    public static final int FROM_BUY_DIAMOND = 2;

    /**
     * 回调
     */
    private ICommonBuyCallBack mCallBack;

    /**
     * 上下文
     */
    private Context mContext;

    /**
     * 来自
     */
    private int mIntType = 0;

    /**
     * 剩余的金币数量
     */
    private int mIntGoldCount = 0;

    /**
     * 剩余的星钻数量
     */
    private int mIntGoldDiamond = 0;

    /**
     * 支付相关操作
     */
    private IPayRechargeOperate payOperate;

    /**
     * loading框
     */
    private Dialog mWaitDlg;
    
    /**
     * 返回的代码
     */
    private String mRetCode;
    /**
     * 返回的错误信息
     */
    private String mRetMsg;

    public CommonBuyTask(Context context, int type, ICommonBuyCallBack callBack) {
        this.mContext = context;
        this.mIntType = type;
        this.mCallBack = callBack;

        this.payOperate = PaymentFactory.getInstance().getPayRechargeOperate();

        mWaitDlg = new StarappWaitDlg(mContext, mContext.getString(R.string.pay_loading),
                R.style.Style_Self_Info_Dlg);

        mWaitDlg.setCanceledOnTouchOutside(false);
        mWaitDlg.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                // 屏蔽Search键(因为按搜索按键会被关闭)
                switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_SEARCH:
                    return true;
                case KeyEvent.KEYCODE_BACK:
                    return true;
                default:
                    return false;
                }
            }
        });

        //mWaitDlg.show();
    }

    /**
     * 
     * <br>
     * Created 2014-11-13 上午11:43:22
     * 
     * @author zhangqr
     */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        if (mWaitDlg != null) {
            mWaitDlg.show();
        }
    }

    protected Object doInBackground(Integer... params) {
        if (!CommUtil.JudgeNetWorkStatus(mContext)) {//网络不可用
            
            mRetCode = "-1";
            mRetMsg = mContext.getString(R.string.pay_failed_connect);
            
            return null;
        }
        
        if (FROM_BUY_GOLD == mIntType) {

            mIntGoldDiamond = 0;
            mIntGoldCount = params[0];

            // TODO 调用服务端接口，将星钻兑换成金币
            int rate = StarAppConst.PAY.DEFAULT_RATE;
            try {
                rate = Integer.parseInt(StarAppConfig.getConfig(StarAppConfig.CONFIG_PAY_RATE));
            } catch (Exception e) {

            }
            int diamondCount = mIntGoldCount / rate;
            IPayInfoBean bean = payOperate.diamondToGold(diamondCount);
            mIntGoldDiamond = bean.getStar();
            mIntGoldCount = bean.getGolden();
            
            mRetCode = bean.getErrCode();
            mRetMsg = bean.getErrMsg();

        } else if (FROM_BUY_DIAMOND == mIntType) {

            mIntGoldDiamond = params[0];
            mIntGoldCount = 0;

            // TODO 调用服务端接口，查询星钻和金币余额
            IWealthInfoBean wealthInfo = payOperate.getWealthInfo();
            mIntGoldDiamond = wealthInfo.getStar();
            mIntGoldCount = wealthInfo.getGolden();
            
            mRetCode = wealthInfo.getErrCode();
            mRetMsg = wealthInfo.getErrMsg();
        }

        return null;
    }

    /**
     * 
     * <br>
     * Created 2014-11-6 上午11:28:35
     * 
     * @param result
     * @author zhangqr
     */
    @Override
    protected void onPostExecute(Object result) {
        super.onPostExecute(result);
        dismiss();
        long uid = ApplicationVariable.INSTANCE.getUapUid();
        if (mCallBack != null) {
            // TODO 服务端调用
            if(PayRechargeOperateImpl.DEFAULT_SUCCESS.equalsIgnoreCase(mRetCode)) {
                if (FROM_BUY_GOLD == mIntType) {
                    mCallBack.onSuccess(mIntGoldDiamond, mIntGoldCount);
                } else if (FROM_BUY_DIAMOND == mIntType) {
                    mCallBack.onSuccess(mIntGoldDiamond, mIntGoldCount);
                }
            } else {
                LogUtils.e(TAG, "errCode="+mRetCode+", errMsg="+mRetMsg);
                mCallBack.onFail(mRetMsg);
            }
            
        }
    }

    private void dismiss() {
        if ((mWaitDlg != null) && (mWaitDlg.isShowing())) {
            mWaitDlg.dismiss();
        }
    }

}
